<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title></title>
<#include "/jeecms_sys/head.html"/>
<script type="text/javascript">
$(function() {
	$("#jvForm").validate();
	processButton();
});
var subTopicSize=${activeConfig.lotteryConfigs?size};
function up(subTopicId){
	if(subTopicId!=0){
		var subTopicHtml=$("#subTopic"+subTopicId).html().replace("up('"+subTopicId+"')","up('"+(subTopicId-1)+"')");
		var subBeforeTopicHtml=$("#subTopic"+(subTopicId-1)).html().replace("up('"+(subTopicId-1)+"')","up('"+subTopicId+"')");
		$("#subTopic"+subTopicId).html(subBeforeTopicHtml);
		$("#subTopic"+(subTopicId-1)).html(subTopicHtml);
		//交换排序
		$("input[name='subPriority']").each(function(i){
			$(this).val(i*1+1);
		});
		processButton();
	}
}
function down(subTopicId){
	if(subTopicId!=(subTopicSize-1)){
		var subTopicHtml=$("#subTopic"+subTopicId).html().replace("down('"+subTopicId+"')","down('"+(subTopicId*1+1)+"')");
		var subAfterTopicHtml=$("#subTopic"+(subTopicId*1+1)).html().replace("down('"+(subTopicId*1+1)+"')","down('"+subTopicId+"')");
		$("#subTopic"+subTopicId).html(subAfterTopicHtml);
		$("#subTopic"+(subTopicId*1+1)).html(subTopicHtml);
		//交换排序
		$("input[name='subPriority']").each(function(i){
			$(this).val(i*1+1);
		});
		processButton();
	}
}
function processButton(){
	$("input[id^='upButton']").each(function(i){
		$(this).attr("disabled","");
		$(this).attr("class","up");
		if(i==0){
			$(this).attr("disabled","disabled");
			$(this).attr("class","up_disabled");
		}
		$(this).unbind();
		$(this).bind('click', function() {
			  up(i);
		});		
	});
	$("input[id^='downButton']").each(function(i){
		$(this).attr("disabled","");
		$(this).attr("class","down");
		if(i==subTopicSize-1){
			$(this).attr("disabled","disabled");
			$(this).attr("class","down_disabled");
		}
		$(this).unbind();
		$(this).bind('click', function() {
			  down(i);
		});	
	});
}
</script>
<style type="text/css">
.items{padding-top:1px;}
</style>
</head>
<body onload="previewImg(1);">
<div class="box-positon">
	<div class="rpos"><@s.m "global.position"/>:  <@s.m "activeConfig.function"/> - <@s.m "global.edit"/></div>
	<form class="ropt">
		<input type="submit" class="return-button" value="<@s.m "global.backToList"/>" onclick="this.form.action='v_list.do';"/>
	</form>
	<div class="clear"></div>
</div>
<div class="body-box">
<#include "/common/image_upload.html"/>

<@p.form id="jvForm" action="o_update.do">

<@p.text width="50" colspan="1" label="活动名称" name="name" value=activeConfig.name required="true" class="required" maxlength="255"/>

<@p.select colspan="1" label="shopInfo.shopName" name="shopId" value=activeConfig.shop.id  list=listShop listKey="id" listValue="shopName" class="required" required="true"/><@p.tr/>

<@p.text width="50" colspan="1" label="活动开始时间" name="startTime" value=activeConfig.startTime required="false" class="Wdate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" maxlength="255"/>
<@p.text width="50" colspan="1" label="活动截止时间" name="endTime" value=activeConfig.endTime required="false" class="Wdate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" maxlength="255"/><@p.tr/>

<@p.text width="50" colspan="1" label="每个用户每天抽奖次数" name="lotteryDayCount" value=activeConfig.lotteryDayCount class="digits" maxlength="10"/>
<@p.text width="50" colspan="1" label="每个用户抽奖次数" name="lotteryCount" value=activeConfig.lotteryCount class="digits" maxlength="10"/><@p.tr/>
<@p.text width="50" colspan="2" label="概率最大值" name="pro" id="pro" value=activeConfig.pro class="digits" maxlength="10" help="（概率的分母）例如此次活动以1/10000的基准概率。那这个值就是10000"/><@p.tr/>

<!-- 上传图片 -->
<@p.td label="活动图片" colspan="1" width="50" noHeight="true">
<@p.text id="uploadImgPath1" name="img" value=activeConfig.img style="width:220px" />
<!--  <input type="button" value="<@s.m "imageupload.preview"/>" onclick="previewImg(1);" class="preview-button"/> --><br/>
<span id="ufc1" style="position:relative">
<input type='text' id='uploadFileText1' size="14"/>  
<input class="browse" type='button' value='<@s.m "global.browse"/>'/>
<input onchange="$('#uploadFileText1').val(this.value)" size="14" type="file" id="uploadFile1" class="file-button"/>
</span>
<label><input type="checkbox" onclick="$('#mark1').val(this.checked);"/><@s.m "imageupload.mark"/></label>
<input type="hidden" id="mark1" value="false"/> <input type="button" value="<@s.m "imageupload.upload"/>" onclick="upload(1);" class="upload-button"/><br/>
<@s.m "global.width"/>: <input type="text" id="zoomWidth1" value="100" size="5"/>
<@s.m "global.height"/>: <input type="text" id="zoomHeight1" value="70" size="5"/> 
<input type="button" value="<@s.m "imageupload.cut"/>" onclick="imgCut(1);" class="cut-button"/> 
</@p.td>
<@p.td colspan="1" width="50">
<img id="preImg1" alt="<@s.m "imageupload.preview"/>" style="width:100px;height:70px;background-color:#CCCCCC;border:1px solid #333"/>
</@p.td><@p.tr/>
<!-- 图片end -->
<@p.editor  value=activeConfig.content colspan="2" label="活动简介" name="content"/><@p.tr/>

<@p.hidden name="status" value=activeConfig.status /><@p.tr/>
<@p.hidden id="delLotteryId" name="delLotteryId" /><@p.tr/>

<@p.td colspan="2">
<div>
	<div class="items" > <input type="button" class="add_subTopic" value="增加活动奖项" onclick="addlotteryConfig();"/></div>
	<#assign total="${activeConfig.lotteryConfigs?size}"/>
	<#list activeConfig.lotteryConfigs as lottery>
		<div class="items" id="lottery${lottery_index}" style="text-align:left;">
		<input type="hidden" id="lottery_id${lottery_index}" name="lottery_id${lottery_index}" value="${lottery.id}" style="width:8px" readonly="readonly"/>
		<input type="text" name="num${lottery_index}" value="${lottery_index}" style="width:8px" readonly="readonly"/>
		
		奖品名称: <input type="text" id="lottery_prizeName${lottery_index}" name="lottery_prizeName${lottery_index}" value="${lottery.prizeName!}" style="width:100px"/>
		&nbsp; 奖品数量: <input type="text" id="lottery_maxCount${lottery_index}" name="lottery_maxCount${lottery_index}" value="${lottery.maxCount!}" style="width:100px"/>
		&nbsp; 每天该奖品中奖数量:<input type="text" name="lottery_dayCount${lottery_index}" id="lottery_dayCount${lottery_index}" value="${lottery.dayCount!}" style="width:100px" />
&nbsp; 每个用户抽中该奖品数量:<input type="text" name="lottery_userMaxCount${lottery_index}" id="lottery_userMaxCount${lottery_index}" value="${lottery.userMaxCount!}" style="width:100px"/>
&nbsp; 奖品概率:<input type="text" name="lottery_pro${lottery_index}" id="lottery_pro${lottery_index}" value="${lottery.pro!}" style="width:100px"/>
		<br/>

&nbsp; 中奖位置:<input type="text" name="lottery_index${lottery_index}" id="lottery_index${lottery_index}" value="${lottery.index!}" style="width:200px"/>（请输入整数，多个位置时请用“,”隔开）
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		&nbsp; <input class="del-button" type="button" value="<@s.m "global.delete"/>" onclick="del(${lottery_index});$(this).parent().remove()"/>
		
		</div>
	</#list>
	<div id="subTopicContainer"></div>
</div>
</@p.td>
<@p.tr/>
<@p.td colspan="2"><@p.hidden name="id" value=activeConfig.id/><@p.submit code="global.submit" onclick="return checkLottery()"/> &nbsp; <@p.reset code="global.reset"/></@p.td>
</@p.form>
</div>

<textarea id="lotteryConfig" style="display:none;">
<div class="items" style="text-align:left;">
<input type="text" name="num{0}" value="{0}" style="width:8px" readonly="readonly"/>
奖品名称: <input type="text" name="lottery_prizeName{0}" style="width:100px"/>
&nbsp; 奖品数量: <input type="text" name="lottery_maxCount{0}" id="lottery_maxCount{0}" style="width:100px"/><!--<input type="text" name="subPriority" value="{0}" style="width:30px"/>-->
&nbsp; 每天该奖品中奖数量:<input type="text" name="lottery_dayCount{0}" id="lottery_dayCount{0}" style="width:100px" />
&nbsp; 每个用户抽中该奖品数量:<input type="text" name="lottery_userMaxCount{0}" id="lottery_userMaxCount{0}" style="width:100px"/>
&nbsp; 奖品概率:<input type="text" name="lottery_pro{0}" id="lottery_pro{0}" style="width:100px"/><br/>

&nbsp; 中奖位置:<input type="text" name="lottery_index{0}" id="lottery_index{0}" style="width:200px"/>（请输入整数，多个位置时请用“,”隔开）
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp; <input class="del-button" type="button" value="<@s.m "global.delete"/>" onclick="$(this).parent().remove();del({0})"/>
<span id="voteItemMsg{0}" style="color: red;"></span>
</div>
</textarea>

<script type="text/javascript">
var delNum=new Array(); ;  //被删除的奖项号
var delLotteryId=new Array(); ;  //被删除的奖项id
var subtpl = $.format($("#lotteryConfig").val());
var subindex = ${activeConfig.lotteryConfigs?size};
function addlotteryConfig() {
	$("#subTopicContainer").append(subtpl(subindex++));
}
function changeAddItemButton(index,value){
	if(value==3){
			$("#addItem"+index).attr("disabled","disabled");
			$("#itemsContainer"+index).html("");
			$("#voteItemMsg"+index).html("");
		}else{
			$("#addItem"+index).attr("disabled","");
			}
}
function checkVoteItem(){
	var subIndexId,subTypeName,subTypeObject,flag=true,voteItemMsgId;
	$("input[name^='subtitle']").each(function(){
			subIndexId=$(this).attr("name").split("subtitle")[1];
			subTypeName="typeId"+subIndexId;
			$("input[name='"+subTypeName+"']").each(function(i){
				subTypeObject=$(this);
				if(subTypeObject.attr("checked")&&subTypeObject.val()!=3){
					var i=0;
					$("input[id^='"+subIndexId+"']").each(function(){
							i++;
						});
					if(i==0){
							$("#voteItemMsg"+subIndexId).html("<@s.m 'cmsVoteItem.hasNoOne'/>");
							flag= false;
						}
				}
			});
		});
	return flag;
}
function del(num){     //记录删除了的将项编号
delNum.push(num);
var lotterId = document.getElementById("lottery_id"+num).value;
delLotteryId.push(lotterId);
document.getElementById("delLotteryId").value=delLotteryId;
}
function checkLottery(){
//验证数量正整数
delNum.sort();
var isDel;
var sumPro=0;
for(var i=0;i<subindex;i++){
  isDel=false;
  for(var j=0;j<delNum.length;j++){
    if(delNum[j]==i){
      isDel=true;
    }
  }
  if(isDel==false){  //如果i没有被删除,去验证它，否则不做处理继续循环
  var maxCount=document.getElementById("lottery_maxCount"+i).value;
  var dayCount=document.getElementById("lottery_dayCount"+i).value;
  var userMaxCount=document.getElementById("lottery_userMaxCount"+i).value;
  var pro=document.getElementById("lottery_pro"+i).value;
     var index=document.getElementById("lottery_index"+i).value;
  
  if(pro==""){
     sumPro=parseFloat(sumPro)+parseFloat(0);
  }else{
   sumPro=parseFloat(sumPro)+parseFloat(pro);
   }
  if(maxCount!=""&&!isPInt(maxCount)){
  alert("奖品数量必须是正整数！");
  return false;
  }
   if(dayCount!=""&&!isPInt(dayCount)){
  alert("每天该奖品中奖数量必须是正整数！");
  return false;
  }
   if(userMaxCount!=""&&!isPInt(userMaxCount)){
  alert("每个用户抽中该奖品数量必须是正整数！");
  return false;
  }
   if(pro!=""&&isNaN(pro)){
  alert("奖品概率必须是数字！");
  return false;
  }
   if(index==null||index==""){
  alert("中奖位置不能为空！");
  return false;
  }else{
  var indexS=index.split(",");
  for(var n=0;n<indexS.length;n++){
  if(!isZS(indexS[n])){
  alert("中奖位置必须是整数！");
  return false;
  }
  }
  }
  }
}
var activePro=document.getElementById("pro").value;
if(sumPro>activePro){
alert("奖项概率之和不能大于活动概率！");
return false;
}
	return true;
}
function isPInt(str) {  //验证正整数
    var g = /^[1-9]*[1-9][0-9]*$/;
    return g.test(str);
}
function isNumber(str) {  //验证数字
    var g = /^[0-9]+$/;
    return g.test(str);
}
function isZS(str){
 var g=/^\d+$/;
 return g.test(str);
}
</script>

</body>
</html>